import { handleRouteNavigateTo, handleShowToast } from '@/core/utils';
import { NavLinkEnum } from '@/core/enum';
import { queryReceiveCoupon } from '@/core/api';

export const handleAdvertiseDetail = (type : number | string, val : string) => {
	if (!val) {
		return ''
	}
	if (type == NavLinkEnum.urlLink) {  /** url类型的链接 */
		if (val.includes('http')) {
			// #ifdef APP-PLUS || APP-PLUS-NVUE || MP
			handleRouteNavigateTo(`/pages/subpackage/webview/webview?src=${val}`);
			// #endif
		} else {
			// #ifdef APP-PLUS || APP-PLUS-NVUE || MP
			if (val == '/pages/home/home' ||
				val == '/pages/cart/cart' ||
				val == '/pages/classify/classify' ||
				val == '/pages/member/member'
			) {
				uni.reLaunch({ url: val });
			} else if (val.includes('/pages/subpackage/coupon/coupon?id=')) {
				let id = val.replace('/pages/subpackage/coupon/coupon?id=', "");
				receiveCoupon(id);
			} else {
				if (val.includes('pages')) {
					handleRouteNavigateTo(val);
				}
			}
			// #endif
		}
	} else if (type == NavLinkEnum.shop) {  /** 商品类型的链接 */
		handleRouteNavigateTo(`/pages/goods/detail?id=${val}`);
	} else if (type == NavLinkEnum.article) {  /** 文章类型的链接 */
		handleRouteNavigateTo(`/pages/subpackage/article/detail/detail?id=${val}&idType=1`);
	} else if (type == NavLinkEnum.articleCategory) {  /** 文章列表类型的链接 */
		handleRouteNavigateTo(`/pages/subpackage/article/category/category?id=${val}`);
	} else if (type == NavLinkEnum.intelligentForms) {  /** 自定义表单类型的链接 */
		handleRouteNavigateTo(`/pages/subpackage/form/detail/detail?id=${val}`);
	} else if (type == NavLinkEnum.shopCategory) {  /** 商品分类类型的链接 */
		handleRouteNavigateTo(`/pages/category/category?id=${val}`);
	} else if (type == NavLinkEnum.wxMiNiProgram) {  /** 小程序类型的链接 */
		uni.navigateToMiniProgram({
			appId: val,
			path: '',
			extraData: {},
			success(res : any) {
				console.log("跳转小程序成功", res);
			}, fail(res : any) {
				console.log("跳转小程序失败", res);
			}
		})
	} else {
		if (val.includes('pages')) {
			handleRouteNavigateTo(val);
		}
	}
}

/** 用户领取优惠券 */
export const receiveCoupon = async (id : string | number) => {
	uni.showLoading({
		title: '加载中',
		mask: true
	});

	const receiveCoupon = await queryReceiveCoupon({ id, });
	if (receiveCoupon.status) {
		handleShowToast(receiveCoupon.msg, 'success')
	} else {
		handleShowToast(receiveCoupon.msg)
	}
	uni.hideLoading();
}